package com.blockchain.securewallet.activities;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import com.blockchain.securewallet.R;
import com.blockchain.securewallet.WalletApp;
import com.blockchain.securewallet.base.BaseActivity;
import com.blockchain.securewallet.dialog.IDialogListener;
import com.blockchain.securewallet.dialog.UniversalDialog;
import com.blockchain.securewallet.utils.StringUtil;
import com.blockchain.securewallet.utils.UIHelper;
import com.wangnan.library.GestureLockView;
import com.wangnan.library.listener.OnGestureLockListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LockActivity extends BaseActivity implements OnGestureLockListener, IDialogListener {

    @BindView(R.id.gesture_lock)
    GestureLockView lockView;

    @BindView(R.id.tv_unlock_other)
    TextView tv_unlock_other;

    @BindView(R.id.tv_tips_unlock_error)
    TextView tv_tips_unlock_error;

    private String password;
    private int err_count = 5;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lock);
        ButterKnife.bind(this);
        password = WalletApp.getInstance().getLockPassword();

        lockView.setGestureLockListener(this);

        UIHelper.expandViewTouchDelegate(tv_unlock_other, 60, 30, 60, 60);
    }

    @Override
    public void onBackPressed() {
        //不能让她退出去
    }

    @OnClick(R.id.tv_unlock_other)
    void forget() {
        gotoUnlockActivity();
//        finish();
    }

    private void gotoUnlockActivity() {
        Intent intent = new Intent();
        intent.setClass(this, UnLockWalletActivity.class);
        intent.putExtra("from", UnLockWalletActivity.FROM);
        startActivity(intent);
    }

    @Override
    public void onStarted() {

    }

    @Override
    public void onProgress(String progress) {

    }

    @Override
    public void onComplete(String result) {
        if (password.equals(result)) {
            if (showPwDialog()) return;
            finish();

        } else {
            err_count--;
            if (showPwDialog()) return;

            tv_tips_unlock_error.setText(StringUtil.getResourceStringAndFormat(R.string.tips_lock_gesture_unlock_error, err_count));
            tv_tips_unlock_error.setVisibility(View.VISIBLE);

            //lockView.clearView(); // 下方的方法已清
            lockView.showErrorStatus(1200);

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    //这里是主线程,关掉
                    tv_tips_unlock_error.setVisibility(View.GONE);
                }
            }, 1200);
        }
    }

    private boolean showPwDialog() {
        if (err_count <= 0) {
            lockView.clearView();

            String tips = StringUtil.getResourceString(R.string.tips_lock_dialog);
            String okText = StringUtil.getResourceString(R.string.tips_lock_by_pw_please);
            UniversalDialog.showDoubleTipsDialog(getSupportFragmentManager(), this, tips, okText, true);
            return true;
        }
        return false;
    }


    @Override
    public void onItemClick(String... text) {
        //ToastUtils.showToast(text1 + "\n" + text2);
        gotoUnlockActivity();
    }


}
